<?PHP
/**
 * @dbOrganization.php
 * Controls database operations on the dborganization table.
 *
 * Copyright 2011 by Matthew Wakim.  This program is part of Donate.
 * Donate is free software.  It comes with absolutely no warranty.
 * You can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
 *
 * @version March 5th 2011
 * @author Matthew Wakim
 */
/** this function users the organization.php file once in this file
 */
include_once('dbConnect.php');
include_once('../model/Organization.php');
Class dbOrganization {

	/**
	 *this function sets up the database for donor.
	 *@param
	 *@return
	 */
	static function createTable() {

		$query =("CREATE  TABLE IF NOT EXISTS organization (
  donorID         INT 			NOT NULL ,
  organizationName VARCHAR(100) NULL ,
  description      VARCHAR(100) NULL ,

CONSTRAINT pk_organization PRIMARY KEY (donorID),
 CONSTRAINT fk_organization_donor FOREIGN KEY (donorID)
	REFERENCES donor (donorID)ON DELETE CASCADE ON UPDATE CASCADE
);
  ");

		connect();
		$result = mysql_query($query);
		echo mysql_error();
		mysql_close();
		if ($result == false) {
			return false;
		}

		//Previous query was successful.
		return true;
	}
	/** this function gets the donorID field from its location
	 */
	function getdonorID($d) {
		$result = mysql_query("SELECT donorID FROM Organization WHERE donorID='$d'");
		$row = mysql_fetch_row($result);
		return $row[0];
	}
	/** this function gets the organizationName field from its location
	 */
	function getOrganizationName($d) {
		$result = mysql_query("SELECT OrganizationName FROM Organization WHERE donorID='$d'");
		$row = mysql_fetch_row($result);
		return $row[0];
	}
	/** this function gets the Description field from its location
	 */
	function getDescription($d) {
		$result = mysql_query("SELECT description FROM Organization WHERE donorID='$d'");
		$row = mysql_fetch_row($result);
		return $row[0];
	}


	static function addOrganizationByFields($donorID,$organizationName,$description, &$id)
	{
		$o = new Organization($donorID,$organizationName,$description);
		$result = dbOrganization::addOrganization($o);
		if ($result == false) {
			die ("Organization could not be updated with specified information.<br />");
		}
	}

	static function verifyInsertedOrganization($id)
	{
		$output = "";
		$error = dbOrganization::getAllByID($id, $output);
		if ($error == true) {
			printf("%s<br />", $output);
		}
		else {
			$ind = new Organization($id, $output['organizationName'], $output['description']);
			printf("Inserted: %s.<br />", $ind->printAllAttributes( ));
		}
	}
	
  static function getAllOrganizations(&$result) {
    connect( );
    $result = mysql_query("SELECT * FROM Organization");
    mysql_close( );
    return $result;
  }
  
	/** this function adds all the fields in the organization table to the database
	 */
	static function addOrganization($v) {
		if (!$v instanceof Organization) {
			echo ("Invalid arguement insertInto ( ) function<br />");
			return false;
		}
		//Compose the INSERT INTO query srting. Primary key value is missing.
		//Database server automatically generates a unique ID for the primary Key.
		$query = sprintf("INSERT INTO organization VALUES(%d, '%s', '%s')",
		$v->getDonorID(),
		$v->getOrganizationName(),
		$v->getDescription()
		);
		connect( );
		$result = mysql_query($query);
		mysql_close( );
		if ($result == false) {
			echo mysql_error();
			return false;
		}
		//query was successful.
		return true;
	}

	/** this function deletes a particualar organization from the database
	 */
	function deleteOrganization($d) {
		$i = $v->getdonorID();
		return mysql_query("DELETE FROM Organization WHERE donorID='$d'");
	}

	public static function getAllByID($id, &$output) {
		$query = sprintf("SELECT organizationName, description FROM organization WHERE donorID=%d",
		$id);
		// printf("->Query: %s<br />", $query);

		connect( );
		$result = mysql_query($query);
		mysql_close( );

		if ($result == false) {
			$output = sprintf("%s", mysql_error());
			return false;
		}
		//process result and print out warning or attribute values
		return dbOrganization::checkForOneInstance($result, $id, $output);
	}

	private static function checkForOneInstance($result, $id, &$output)
	{
		//Local variables are initialized.
		//Think positively about what this checking returns :-) and set $errorStatus
		//to initial value false
		$errorStatus = false;

		$howMany = mysql_num_rows($result);
		// printf("-->Found %d instance(s) for donorID=%d.<br />", $howMany, $id);
		switch ($howMany) {
			case 0:
				$output = sprintf("No Organization instance with ID=%d was found.",
                          $id);
        $errorStatus = true;
        break;
      case 1:
        //mysql_fetch( ) DOES NOT return primary key values!
        $output = mysql_fetch_assoc($result);
        $errorStatus = false;
        break;
      default:
        $output = sprintf("There are multiple Organization instances 
                           with ID=%d.", $id);
        $errorStatus = true;
        break;
    }
    return $errorStatus;
  }
}